var express = require("express");
var Web3 = require("web3");
var fs = require("fs");

var web3;
if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
  } else {
    // set the provider you want from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider("http://115.238.146.143:8545"));
  }

  var abi = JSON.parse(fs.readFileSync("Works.json").toString());

  var MyContract = web3.eth.contract(abi);
  var myContractInstance = MyContract.at('0xea12cD4171efB697f8340B1802B0E3408B1C5648');
  //function mint(string memory uri) public returns (uint256)  url为作品数据的保存路径, {from: web3.eth.accounts[0], gas: 2000000})暂时写死
  //var result = myContractInstance.mint("233", {from: web3.eth.accounts[0], gas: 2000000});
  // 根据作品Id获取作品信息，作品Id从0开始累加
  //var result = myContractInstance.fetchWorksFromId(1)
  // 返回作品总数
  //var result = myContractInstance.worksCount();
  // 返回某用户所有的作品Id
  var result = myContractInstance.findWorksFromUser(web3.eth.accounts[0]);
  // 交易
  // function buyItem(workId, seller, buyer)
  // {
  //   var result = myContractInstance.buyItem(workId, buyer, {from: seller, gas: 2000000});
  //   return result;
  // }
  // var a = buyItem(1, web3.eth.accounts[2], web3.eth.accounts[3]);
  // console.log(a) ;
  // 删除作品, 查出来的Id=0就表示已经被删了，需要把返回数组中的0过滤掉。
  //var result = myContractInstance.burn(2, web3.eth.accounts[0], {from: web3.eth.accounts[1], gas: 2000000});
  console.log(result);



